﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.ViewModels.DialogViewModels/AddUserDialogModel
* 类       名: AddUserDialogModel
* 功      能:  TD
* 时      间:  2024-05-22 15:15:39
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using Prism.Commands;
using Prism.Mvvm;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using WaferSelectV3.Common;
using WaferSelectV3.DAL.DataEntity;
using WaferSelectV3.DAL.LocalDataAccess;
using WaferSelectV3.Models;

namespace WaferSelectV3.ViewModels.DialogViewModels
{
    public class AddUserDialogModel : BindableBase, IDialogAware
    {
        public DelegateCommand SaveCommand { get; private set; }
        public DelegateCommand CancelCommand { get; private set; }

        public AddUserDialogModel()
        {
            SaveCommand = new DelegateCommand(DoSave);
            CancelCommand = new DelegateCommand(DoCancel);
        }

        private void DoCancel()
        {
            RequestClose?.Invoke(new DialogResult());
        }

        private void DoSave()
        {
            if (string.IsNullOrWhiteSpace(Username) || string.IsNullOrWhiteSpace(Password))
            {
                MessageBox.Show("用户名和密码不能为空", "用户名和密码错误", MessageBoxButton.OK, MessageBoxImage.Error);
                return;
            }
            else
            {
                // 1. 先获取角色类型的值
                var userAdded = new UserEntity()
                {
                    UserName = Username,
                    Password = Password,
                    Role = (int)Enum.Parse(typeof(RoleType), RoleList![SelectedIndex]),
                    CreateBy = GlobalValues.CurrentLoginUser.UserName,
                    CreateTime = DateTimeHelper.GetTimeNowForSql(),
                    LastLoginTime = "无"
                };
                var result = UserAccess.AddDataSingle(userAdded);
                if (!result.State)
                {
                    MessageBox.Show(result.Message!, "数据库操作失败", MessageBoxButton.OK, MessageBoxImage.Error);
                    return;
                }
            }
            RequestClose?.Invoke(new DialogResult());
        }

        private string? _username;

        public string? Username
        {
            get { return _username; }
            set
            {
                _username = value;
                RaisePropertyChanged();
            }
        }

        private string? _password;

        public string? Password
        {
            get { return _password; }
            set
            {
                _password = value;
                RaisePropertyChanged();
            }
        }

        private string? _title;

        public string? Title
        {
            get { return _title; }
            set
            {
                _title = value;
                RaisePropertyChanged();
            }
        }

        private ObservableCollection<string>? _roleList;

        public ObservableCollection<string>? RoleList
        {
            get { return _roleList; }
            set
            {
                _roleList = value;
                RaisePropertyChanged();
            }
        }

        private int _selectedIndex;

        public int SelectedIndex
        {
            get { return _selectedIndex; }
            set
            {
                _selectedIndex = value;
                RaisePropertyChanged();
            }
        }


        public event Action<IDialogResult>? RequestClose;

        public bool CanCloseDialog()
        {
            return true;
        }

        public void OnDialogClosed()
        {
            RoleList!.Clear();
            RoleList = null;
        }

        public void OnDialogOpened(IDialogParameters parameters)
        {
            if (parameters != null)
            {
                parameters.TryGetValue("Title", out string titleValue);
                if (!string.IsNullOrEmpty(titleValue))
                {
                    Title = titleValue;
                }
            }
            // 创建默认要显示的用户信息
            RoleList = new ObservableCollection<string>()
            {
                Enum.GetName(typeof(RoleType),RoleType.操作员)!,
                Enum.GetName(typeof(RoleType),RoleType.管理员)!,
                Enum.GetName(typeof(RoleType),RoleType.工程师)!
            };
            SelectedIndex = 0;
        }

    }
}
